package com.dk.controller;

import com.dk.pojo.Car;
import com.dk.service.CarServiceI;
import com.dk.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("car")
public class CarController {


    @Autowired
    private CarServiceI carServiceI;


    @GetMapping
    @ApiOperation(value = "查询汽车信息",httpMethod = "GET",response = Result.class)
    public Result findCarList(){

            List<Car> carList = carServiceI.findAll();
            return Result.ok().setDate(carList);

    }


    //根据id查询
    @GetMapping("/{carId}")
    @ApiOperation(value = "根据Id查询汽车信息",httpMethod = "GET",response = Result.class)
    @ApiImplicitParam(name = "carId",value = "汽车的id",required = true,paramType = "path",dataTypeClass = Integer.class)
    @ApiResponse(code = 200,message = "success")
    public Result findCarList(@PathVariable("carId")Integer carId){
        Car car = carServiceI.findById(carId);
        return Result.ok().setDate(car);
    }

    //根据id删除
    @DeleteMapping("/{carId}")
    @ApiOperation(value = "根据Id删除汽车信息",httpMethod = "DELETE")
    @ApiImplicitParam(name = "carId",value = "汽车的id",required = true,paramType = "path",dataTypeClass = Integer.class)
    @ApiResponse(code = 200,message = "success")
    public void deleteCarList(@PathVariable("carId")Integer carId){
        carServiceI.deleteById(carId);
    }


    //修改
    @PutMapping
    @ApiOperation(value = "根据Id修改汽车信息",httpMethod = "PUT")
    @ApiResponse(code = 200,message = "success")
    public Result updateCar(@RequestBody Car car){
        carServiceI.updateCar(car);
        return Result.ok();
    }

    //增加
    @PostMapping
    @ApiOperation(value = "增加汽车信息",httpMethod = "POST")
    @ApiResponse(code = 200,message = "success")
    public Result saveCar(@RequestBody Car car ){
        carServiceI.saveCar(car);
        return Result.ok();
    }
}
